package net.qhzw.geologr5.gis.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.geologr5.gis.common.framework.utils.JsonResult;
import net.qhzw.geologr5.gis.service.system.entity.Legend;
import net.qhzw.geologr5.gis.service.system.mapper.LegendMapper;
import net.qhzw.geologr5.gis.service.system.query.LegendQuery;
import net.qhzw.geologr5.gis.service.system.service.LegendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class LegendServiceImpl implements LegendService {


    @Autowired
    private LegendMapper legendMapper;


    /****
     * 图例查询
     * @param legendQuery
     * @return
     */
    @Override
    public JsonResult select(LegendQuery legendQuery) {
        try {
            QueryWrapper<Legend> queryWrapper = new QueryWrapper<>();
            List<Legend> legends = legendMapper.selectList(queryWrapper);
            return JsonResult.success(legends);
        } catch (Exception e) {
            log.error("图例查询失败==LegendServiceImpl==method:select==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }

    }


    /***
     * 添加修改图例
     * @param legend
     * @return
     */
    @Override
    public JsonResult edit(Legend legend) {
        try {
            if (legend.getId() != null && legend.getId() > 0) {
                int row = legendMapper.updateById(legend);
                if (row > 0)
                    return JsonResult.success("UPDATE SUCCESS");
                return JsonResult.error("UPDATE ERROR");
            }
            int row = legendMapper.insert(legend);
            if (row > 0)
                return JsonResult.success("INSERT SUCCESS");
            return JsonResult.error("INSERT ERROR");
        } catch (Exception e) {
            log.error("编辑图例失败==LegendServiceImpl==method:edit==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 删除图例
     * @param ids
     * @return
     */
    @Override
    public JsonResult delete(Integer[] ids) {
        try {
            List<Integer> idList = Arrays.asList(ids);
            int row = legendMapper.deleteBatchIds(idList);
            if (row > 0)
                return JsonResult.success("DELETE SUCCESS");
            return JsonResult.error("DELETE ERROR");
        } catch (Exception e) {
            log.error("删除图例失败==LegendServiceImpl==method:delete==errMsg: {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }

}
